package ss.pku.jsa.type.test;

import ss.pku.jsa.type.inference.Analysis;
import ss.pku.jsa.type.inference.JqualPlugin;
import ss.pku.jsa.type.inference.Profile;

public class Result {
	String name;
	double kloc;
	long numClasses;
	long numMethods;
	long numMethodPositions;
	long numMethodPositionsRO;
	long time;
	long maxMemJava;
	long maxMemTotal;
	char libs;
	int numReadonlyAnnotationsInferred;
	int numReadonlyAnnotations;
	int numFields;
	int numFieldsFinal;

	public Result(String name, double d, Analysis a) {
		this.name = name;
		this.kloc = d;
		this.numClasses = a.numOriginalClasses;
		this.numMethods = a.numOriginalMethods;
		if (JqualPlugin.inferReadonly) {
			Analysis.JavariResults results = a.getJavariResults();
			this.numMethodPositions = results.numMethodPositions;
			this.numMethodPositionsRO = results.numMethodPositionsRO;

			this.numReadonlyAnnotationsInferred = a.readonlyTextualAnnotationsInferred
					.size();
			this.numReadonlyAnnotations = a.readonlyAnnotations.size();

			this.numFields = results.numFields;
			this.numFieldsFinal = results.numFieldsFinal;
		}
		this.time = Profile.elapsedTime();
		this.maxMemJava = Profile.maxMem;
		this.maxMemTotal = Profile.procMemoryKB();
		this.libs = 'N';
		if ((JqualPlugin.useClosure) && (JqualPlugin.useJavalib))
			this.libs = 'F';
	}

	public void tableLine() {
		StringBuilder out = new StringBuilder();
		if (JqualPlugin.banshee)
			out.append("BANSHEE_");
		out.append("RESULT:|" + this.name + "|");
		out.append(String.format("%3.1fKLOC|",
				new Object[] { Double.valueOf(this.kloc) }));
		out.append(this.numClasses + "|");
		out.append(this.numMethods + "|");
		out.append(this.numMethodPositionsRO + "/" + this.numMethodPositions
				+ "|");
		out.append(this.numReadonlyAnnotationsInferred + "/"
				+ this.numReadonlyAnnotations + "|");
		out.append(this.numFieldsFinal + "/" + this.numFields + "|");
		out.append(Profile.longToTime(this.time) + "|");
		out.append(this.maxMemJava / 1000000L + "MB|");
		out.append(this.maxMemTotal / 1000000L + "MB|");
		out.append(this.libs + "|");
		out.append("\n");
		JqualPlugin.out.println(out.toString());
	}

	public String toString() {
		StringBuilder out = new StringBuilder();
		out.append("Analysis of " + this.name + "\n");
		out.append("Size: " + this.kloc + "KLOC" + "\n");
		out.append("Total number of visited classes: " + this.numClasses + "\n");
		out.append("Total number of visited methods: " + this.numMethods + "\n");
		out.append("Time: " + Profile.longToTime(this.time)
				+ "\n--------------------------------\n");
		return out.toString();
	}

	public String javariSummary() {
		StringBuilder out = new StringBuilder();

		out.append("Total number of possible readonly positions: "
				+ this.numMethodPositions + "\n");
		out.append("Total number of positions inferred readonly: "
				+ this.numMethodPositionsRO + "\n");
		out.append("Total number of Fields: " + this.numFields + "\n");
		out.append("Total number of final Fields: " + this.numFieldsFinal
				+ "\n");
		return out.toString();
	}

	public String ffiSummary() {
		StringBuilder out = new StringBuilder();
		out.append("-----------FFI Stats---------\n");
		out.append("Number of places for annotations: "
				+ JqualPlugin.analysis.ffiNumPotentialAnnotations());
		out.append("\n");
		out.append("Number of annotations: "
				+ JqualPlugin.analysis.ffiNumAnnotations);
		out.append("\n");
		out.append("Number of F/S annotations: "
				+ JqualPlugin.analysis.fieldSensitiveFields.size());
		out.append("\n");
		return out.toString();
	}

	public void ffiLog() {
	}
}
